package service.impl;

import dao.AdminDao;
import dao.impl.AdminDaoImpl;
import model.bo.*;
import model.InnerResult;
import model.po.Admin;
import model.vo.AdminLoginVO;
import service.AdminService;

import java.util.List;

public class AdminServiceImpl implements AdminService {
    AdminDao adminDao = new AdminDaoImpl();
    private static final Object phantomLock = new Object();

    /**
     * 验证管理员登录信息
     * @param admin
     * @return
     */
    @Override
    public InnerResult validateLoginInfo(AdminBO admin) {
        Admin adminToDb = new Admin();
        adminToDb.setEmail(admin.getEmail());
        Admin adminFromDb = adminDao.getAdmin(adminToDb);

        if (null == adminFromDb) { //账号不存在
            return InnerResult.reject("该账号不存在");  //如果adminFromDb是null需要在方法里直接返回，如果不返回到下面抛出NullPointerException，idea无法定位到这里
        }

        if (!adminFromDb.getPwd().equals(admin.getPwd())) { //账号密码不匹配，
            return InnerResult.reject("密码不正确！");
        } else { //账号密码匹配
            AdminLoginVO loginResponseData = new AdminLoginVO();
            loginResponseData.setToken(adminFromDb.getNickname());
            loginResponseData.setName(adminFromDb.getNickname());
            return InnerResult.fulfill(loginResponseData);
        }
    }

    /**
     * 获取系统中全部的管理员资料
     * @return
     */
    @Override
    public InnerResult getAllAdmins() {
        List<Admin> allAdmins = adminDao.getAllAdmins();
        return InnerResult.fulfill(allAdmins);
    }

    /**
     * 删除管理员
     * @param id
     * @return
     */
    @Override
    public InnerResult deleteAdmins(String id) {
        synchronized (phantomLock) {
            if (2 > adminDao.getAdminsNumber()) {
                return InnerResult.reject("删除失败，系统应至少有一个管理员账户");
            }
            int result = adminDao.deleteAdmin(id);
            return result > 0 ? InnerResult.fulfill() : InnerResult.reject("删除失败");
        }
    }

    /**
     * 更改本次登录的管理员的密码
     * @param admin
     * @return
     */
    @Override
    public InnerResult changePwd(AdminChangePwdBO admin) {
        //管理员修改密码不常用，原本可以用一次数据库请求的，为了不在DAO层引入新类型，这里用两次请求完成
        Admin adminToDb = new Admin();
        adminToDb.setNickname(admin.getAdminToken());
        Admin adminFromDb = adminDao.getAdmin(adminToDb);

        if (null == adminFromDb) {
            return InnerResult.reject("无效的Token");
        }

        if (!adminFromDb.getPwd().equals(admin.getOldPwd())) {
            return InnerResult.reject("原密码不正确");
        }

        adminToDb.setPwd(admin.getNewPwd());
        int result = adminDao.changePwd(adminToDb);
        return result > 0 ? InnerResult.fulfill() : InnerResult.reject("修改密码失败");
    }

    /**
     * 修改管理员账号资料
     * @param admin
     * @return
     */
    @Override
    public InnerResult updateAdminss(AdminBO admin) {
        Admin adminToDb = new Admin();

        adminToDb.setNickname(admin.getNickname());
        Admin adminFromDb = adminDao.getAdmin(adminToDb);
        if (null != adminFromDb && !adminFromDb.getId().equals(admin.getId())) {
            return InnerResult.reject("昵称已被占用");
        }

        adminToDb.setNickname(null);
        adminToDb.setEmail(admin.getEmail());
        adminFromDb = adminDao.getAdmin(adminToDb);
        if (null != adminFromDb && !adminFromDb.getId().equals(admin.getId())) {
            return InnerResult.reject("邮箱已被占用");
        }

        adminToDb.setNickname(admin.getNickname());
        adminToDb.setEmail(admin.getEmail());
        adminToDb.setPwd(admin.getPwd());
        adminToDb.setId(admin.getId());
        int result = adminDao.updateAdmin(adminToDb);
        return result > 0 ? InnerResult.fulfill("修改成功") : InnerResult.reject("修改失败");
    }

    /**
     * 获取id对应的管理员账号资料
     * @param id
     * @return
     */
    @Override
    public InnerResult getAdminsInfo(String id) {
        Admin admin = new Admin();
        admin.setId(Integer.parseInt(id));
        Admin adminFromDb = adminDao.getAdmin(admin);

        if (null == adminFromDb) {
            return InnerResult.reject("查无此人");
        }
        AdminBO adminToData = new AdminBO();
        adminToData.setId(adminFromDb.getId());
        adminToData.setNickname(adminFromDb.getNickname());
        adminToData.setEmail(adminFromDb.getEmail());
        adminToData.setPwd(adminFromDb.getPwd());
        return InnerResult.fulfill(adminToData);
    }

    /**
     * 添加管理员
     * @param admin
     * @return
     */
    @Override
    public InnerResult addAdminss(AdminBO admin) {
        Admin adminToDb = new Admin();

        adminToDb.setNickname(admin.getNickname());
        Admin adminFromDb = adminDao.getAdmin(adminToDb);
        if (null != adminFromDb) {
            return InnerResult.reject("昵称已被占用");
        }

        adminToDb.setNickname(null);
        adminToDb.setEmail(admin.getEmail());
        adminFromDb = adminDao.getAdmin(adminToDb);
        if (null != adminFromDb) {
            return InnerResult.reject("邮箱已被占用");
        }

        adminToDb.setNickname(admin.getNickname());
        adminToDb.setEmail(admin.getEmail());
        adminToDb.setPwd(admin.getPwd());
        int result = adminDao.addAdmin(adminToDb);
        return result > 0 ? InnerResult.fulfill() : InnerResult.reject("创建管理员失败");
    }

    /**
     * 条件筛选查询管理员账号信息
     * @param admin
     * @return
     */
    @Override
    public InnerResult getSearchAdmins(AdminBO admin) {
        Admin adminToDb = new Admin();
        adminToDb.setEmail(admin.getEmail());
        adminToDb.setNickname(admin.getNickname());

        List<Admin> adminList = adminDao.getSearchAdmins(adminToDb);
        return InnerResult.fulfill(adminList);
    }



    //Private Methods


}
